package com.scqkxx.gamebox.activity.activities.presenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scqkxx.gamebox.activity.MainActivity;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.activities.view.ActiveView;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import java.util.List;

/**
 * Created by admin on 2018/8/9. 15点54分
 * name: ts
 * 用途：
 */

public class ActivePersenter {
    private AVObject bean; //当前点击的bean 对象
    private Context mContext; //上下文对象
    private ActiveView mActiveView;
    private int limit = 10; //请求的条数

    public ActivePersenter( Context mContext , ActiveView mActiveView ) {
        this.mContext = mContext;
        this.mActiveView = mActiveView;
    }

    //RecyclerView 设置监听
    public void addOnItemTouchListener( RecyclerView mRecyclerView ) {
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick( BaseQuickAdapter adapter, View view, int position ) {
                bean = (AVObject) adapter.getItem( position );
//                ActivityUtil.switchTo(mContext, MainActivity.class);
            }
        });
    }

    //子控件的监听
    public void OnItemChildClickListener( RecyclerView mRecyclerView ) {
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick( BaseQuickAdapter adapter, View view, int position ) {
                bean = (AVObject) adapter.getItem(position);
                switch (view.getId() ) {
                    case R.id.tv_participate :
                        //马上参与
                        ActivityUtil.switchTo( mContext , MainActivity.class);
                        break;
                }
            }
        });
    }

    /**
     * 进行网络请求的活动
     * @param pageNumber 进行分页的number
     */
    public void sendHttpActiveData( final int pageNumber ){
        AVQuery<AVObject> query = AVQuery.getQuery("A_DxtMarketingBatch");
//        query.whereEqualTo("orderby" , "order desc" );
//        query.orderByDescending("desc");
        query.whereEqualTo( "isDisable" , 0 );
        query.limit(limit );
        query.skip(pageNumber);
        query.findInBackground( new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e ) {
                if ( mActiveView != null ) {
                    if ( e == null ) {
                        mActiveView.onFinishFreshAndLoad(true);
                        Log.e("TAG" , list.toString());
                        if ( pageNumber == 1 ) {
                             mActiveView.clearActiveList(); //清除数据
                        }
                        if  ( !list.isEmpty() && list.size() >= limit ) {
                            mActiveView.setPageNumber();
                            mActiveView.onActiveSuccess(list);
                            mActiveView.loadMoreComplete();
                        } else {
                            //无数据
                            if ( pageNumber == 1 && list.size() == 0 ) {
                                mActiveView.setEmptyStateView();
                            } else {
                                mActiveView.loadMoreEnd();
                            }
                        }
                    } else {
                         mActiveView.onFinishFreshAndLoad(false);
                        if( mActiveView.getListSize() <= 0 ){
                            mActiveView.setErrorStateView();
                        }else{
                            mActiveView.loadMoreFail();
                        }
                    }
                }
            }
        });
    }

    //title的监听
    public void setOnTitleClick(int mTitleType) {
        if (mActiveView == null) return;
        switch ( mTitleType ){
            case TitleView.CLICK_LIFT:
                mActiveView.setOnTitleShoppingClick();
                break;
            case TitleView.CLICK_RIGHT:
                mActiveView.setOnTitleServiceClick();
                break;
            case TitleView.CLICK_BACK:
                mActiveView.onFinishActivity();
                break;
            case TitleView.CLICK_SETTING:
                mActiveView.setOnTitleSettingClick();
                break;
        }
    }

}
